<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <link rel="shortcut icon" type="image/x-icon" href="http://www.oschina.net/img/favicon.ico">
  <title>Python操作MongoDB(PyMongo模块的使用) - 开源中国社区</title>
  <meta name="Keywords" content="">  <meta name="Description" content="PyMongo模块是Python对MongoDB操作的接口包，代码主要实现对MongoDB的几种操作：增删改查以及排序等功能，网上有很多资料，本文仅充当学习笔记。">  <link rel="stylesheet" href="a_files/style2013.css" type="text/css" media="screen">
  <link rel="stylesheet" href="a_files/channel.css" type="text/css" media="screen">
  <link rel="stylesheet" type="text/css" href="a_files/jquery.css" media="screen">
  <link rel="alternate" type="application/rss+xml" title="最新开源项目" href="http://www.oschina.net/project/rss">
  <link rel="alternate" type="application/rss+xml" title="最新开源资讯" href="http://www.oschina.net/news/rss">
  <link rel="alternate" type="application/rss+xml" title="最新问题列表" href="http://www.oschina.net/question/rss">
  <link rel="alternate" type="application/rss+xml" title="最新翻译列表" href="http://www.oschina.net/translate/rss">
  <link rel="alternate" type="application/rss+xml" title="最新博客列表" href="http://www.oschina.net/blog/rss">
  <link rel="alternate" type="application/rss+xml" title="最新代码分享列表" href="http://www.oschina.net/code/rss">
  <link rel="alternate" type="application/rss+xml" title="开源中国 - 源码列表" href="http://www.oschina.net/code/source_rss">
  <script src="a_files/ca-pub-7090564139599510.js" type="text/javascript" async=""></script><script type="text/javascript" src="a_files/jquery-1.js"></script>
  <script type="text/javascript" src="a_files/jquery_003.js"></script>
  <script type="text/javascript" src="a_files/jquery.js"></script>
    <link rel="stylesheet" href="a_files/tip-yellowsimple.css" type="text/css">
  <script type="text/javascript" src="a_files/jquery_002.js"></script>
  <script type="text/javascript">
  	g_user = {
	id:0,
	name:'',
	login:false};  </script>
    <script type="text/javascript" src="a_files/oschina.js"></script>
  <script type="text/javascript" src="a_files/utils.js"></script>
  <script type="text/javascript" src="a_files/channel.js"></script>
    <link rel="stylesheet" href="a_files/default.css"><link rel="stylesheet" href="a_files/oschina.css"></head>
<body>
<div id="OSC_NavTop">
	<div class="wp998">
        <div id="OSC_Channels">
        	<ul>
        	<li class="item"><a href="http://www.oschina.net/" class="home">首页</a></li>        	<li class="item control_select"><a href="http://www.oschina.net/project" class="project">开源项目</a>
				<ul class="cs_content">					
                	                	<li><a href="http://www.oschina.net/project/lang/19/java">Java 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/194/csharp">C# 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/22/php">PHP 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/21/c">C/C++ 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/26/ruby">Ruby 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/25/python">Python 开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/358/go">Go开源软件</a></li>
                	<li><a href="http://www.oschina.net/project/lang/28/javascript">JS开源软件</a></li>
				</ul>
			</li>
        	<li class="item control_select">
				<a href="http://www.oschina.net/question" class="question">讨论区</a>				
				<ul class="cs_content">					
                	<li><a href="http://www.oschina.net/question?catalog=1"> 技术问答 » </a></li>
                	<li><a href="http://www.oschina.net/question?catalog=2"> 技术分享 » </a></li>
                	<li><a href="http://www.oschina.net/question?catalog=3"> IT大杂烩 » </a></li>
                	<li><a href="http://www.oschina.net/question?catalog=100"> 职业生涯 » </a></li>
                	<li><a href="http://www.oschina.net/question?catalog=4"> 站务/建议 » </a></li>
                	<li><a href="http://www.oschina.net/alipay"> 支付宝专区 » </a></li>
                	<li><a href="http://www.oschina.net/hardware"> 开源硬件专区 » </a></li>
				</ul>
			</li>
        	<li class="item"><a href="http://www.oschina.net/code/list" class="code hl">代码</a></li>
        	        	<li class="item"><a href="http://www.oschina.net/blog" class="blog">博客</a></li>
        	<li class="item"><a href="http://www.oschina.net/translate" class="tran">翻译</a></li>
            <li class="item"><a href="http://www.oschina.net/news" class="news">资讯</a></li>
        	<li class="item control_select">
				<a href="http://www.oschina.net/android" class="mobile">移动开发</a>
				<ul class="cs_content cs_mobile">
                	<li class="android_"><a href="http://www.oschina.net/android">Android开发专区</a></li>
                	<li class="ios_"><a href="http://www.oschina.net/ios/home">iOS开发专区</a></li>
                	<li class="ios_"><a href="http://www.oschina.net/ios/codingList">iOS代码库</a></li>
                	<li class="wp7_"><a href="http://www.oschina.net/wp">Windows Phone</a></li>
				</ul>
			</li>
        	<li class="item t_job"><a href="http://www.oschina.net/job" class="job">招聘</a></li>
									            <li class="item control_select">
                <a href="http://city.oschina.net/" id="MyCities" title="城市圈">城市圈</a>
							</li>
        	</ul>
        </div>
		<div id="OSC_Userbar">
                		    		当前访客身份：游客 [ <a href="http://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1382328_37407">登录</a> | <a href="http://www.oschina.net/home/reg">加入开源中国</a> ]
    				</div>
		<div class="clear"></div>
	</div>
</div>
<div id="OSC_Banner"><div class="wp998"><a href="http://www.oschina.net/" class="Logo" title="OSChina 开源中国">开源中国</a>
<h1><a href="http://www.oschina.net/code/list">代码分享</a></h1>
<dl>
	<dt>当前位置：</dt>
	<dd>
		<a href="http://www.oschina.net/code/list">代码分享</a>&nbsp;»&nbsp;<a href="http://www.oschina.net/code/list/?lang=python">Python</a> &nbsp;»&nbsp;<a href="http://www.oschina.net/code/list/?catalog=database-programming">数据库编程</a>  
	</dd>
</dl>
<form action="http://www.oschina.net/search" class="search">
	<input name="scope" value="code" type="hidden">
	<input id="channel_q" name="q" placeholder="资讯、软件、分享、代码、博客" class="TXT" type="text">
    <button type="submit" class="BTN">搜 索</button>
</form>
<div class="clear"></div></div></div>
<div id="OSC_Screen">
	<div id="OSC_Content" class="CenterDiv">
<script type="text/javascript" src="a_files/scrolltopcontrol.js"></script>
<script type="text/javascript">
  	scrolltotop.offset(100,120);
  	scrolltotop.init();
</script>

<div class="Question Code">
	<div class="Body">
    	<div class="Title">
    		<div class="Asker"><a href="http://my.oschina.net/qingfengkuyu" class="ShowUserOutline" target="_blank"><img src="a_files/1382328_50.jpg" alt="清风苦雨" title="清风苦雨" class="SmallPortrait" user="1382328" align="absmiddle"></a></div>
    		<div class="QTitle">
    			<h1><a href="http://www.oschina.net/code/snippet_1382328_37407">Python操作MongoDB(PyMongo模块的使用)</a></h1>
            	<div class="stat">
            		<a href="http://my.oschina.net/qingfengkuyu">清风苦雨</a> 发布于 2014年07月21日 11时,
            		<a href="#comments">1评/187阅</a>        							
    				            		            	</div>
            </div>
    		<div class="clear"></div>
    	</div>
		<style type="text/css">
#TagsSwitcher{color:#A00;}
#RecTags,#MyTags{margin:5px 0;}
#RecTags a.tag{line-height:24px;}
#favor_form{width:200px;}
#favor_form p:first-child{margin:5px 0;}
#favor_form p {color:#666;}
#favor_form form{min-height:85px;width:200px;}
#favor_form form ._favor_input{display:block;margin:2px 0;width:199px;}
#favor_form form ._favor_button{float:left;padding:2px 5px;}
.favor_ok {text-align:center;font-size:10.5pt;width:199px;height:60px;margin-top:10px;}
#TagsSwitcher{cursor:pointer;float:right;margin-top:10px;}
#MyTags{display:none;width:199px;}
#MyTags a.tag {float:left; background-color: #E0EAF1;border-bottom: 1px solid #3E6D8E;border-right: 1px solid #7F9FB6;color: #3E6D8E;font-size: 8pt;line-height: 16px;margin: 2px 2px 2px 0;padding: 2px 4px;text-decoration: none;white-space: nowrap;}
#MyTags a.tag:hover{background-color: #3E6D8E;color: #FFF;}
#toolbar_wrapper{padding:10px 0 5px 0;}
.toolbar{height:33px;margin:0;padding:0;vertical-align: middle;border:1px solid #CCC;-moz-box-shadow: 0 1px 3px rgba(198,198,198,0.5);-webkit-box-shadow: 0 1px 3px rgba(198,198,198,0.5);box-shadow: 0 1px 3px rgba(198,198,198,0.5);}
.toolbar a{text-decoration:none;color:black;}
.share{float:left;margin:0;padding:3px 0 0 0;line-height:25px;width:280px;}
.share_sina{float:left;background:url(/img/toolbar.gif) -1px -278px no-repeat;padding-left:25px;padding-right:5px;margin-left:3px;}
.share_qq{float:left;background:url(/img/toolbar.gif) -1px -316px no-repeat;padding-left:25px;padding-right:5px;}
.options{float:right;margin:3px 5px 0 5px;padding:0;height:30px;line-height:25px;}
.tool_title {float:left;text-align:center;vertical-align:middle;margin:0 0 0 5px;line-height:33px;}
.options .tool_title{line-height:30px;}
.favorite{float:left;}
.soft_collect{background: url(/img/toolbar.gif) 0 -1px no-repeat; cursor: pointer;padding-left:20px;}
.soft_collected{background: url(/img/toolbar.gif) 1px -28px no-repeat;}
.soft_collect_count{font-size:12pt;font-family: Arial;color:#8B0000;margin:0 10px 0 10px;}
.soft_collect_count:hover {color:red;}
.soft_used{float:right;margin:0 0 0 20px;padding:0;position: relative;}
.soft_used_button{z-index: 1; position: relative; cursor: pointer;margin-left:10px;}
.arrow_holder{background: url(/img/toolbar.gif) -1px -350px no-repeat;}
a.soft_used_button_n{}
a.soft_used_button_y{color:#40AA53;font-weight:bold;}
.soft_used_level{ z-index: 99; position: absolute; top: 28px;left: 0; width: 80px; background-color: #f9f9f9; border: 1px solid #ccc;}
.soft_used_level li{ line-height: 34px; padding-left: 30px;background: url(/img/toolbar.gif) 3px -390px no-repeat; cursor: pointer;}
.soft_used_level li.choice{ background-color: #e9e9e9; background-position: 3px -418px}
.soft_used_level li.level{ background-position: 3px -418px}
.options .report{float:left;background: url(/img/toolbar.gif) 0 -113px no-repeat;padding:0 10px 0 15px;border-top:1px solid #CCC;border-bottom:1px solid #CCC;border-right:1px solid #CCC;}
.options .vote{float:right;width:120px;margin:0 0 0 20px;padding:0 5px;border:1px solid #CCC;text-align:center;vertical-align:middle;}
.options .vote-up-on{float:left;background: url(/img/toolbar.gif) 0 -160px no-repeat;padding-left:15px;}
.options .vote-up-off{float:left;background: url(/img/toolbar.gif) 0 -137px no-repeat;padding-left:15px;}
.options .vote-down-on{float:right;background: url(/img/toolbar.gif) 0 -209px no-repeat;padding-left:15px;}
.options .vote-down-off{float:right;background: url(/img/toolbar.gif) 0 -184px no-repeat;padding-left:15px;}
.options .vote-count-post{font-size:12pt;font-family: Arial;color: #8B0000;text-align: center;vertical-align:middle;margin:0 5px;}
.options .news_deliver{float:left;background: url(/img/toolbar.gif) 3px -243px no-repeat;padding:0 10px 0 24px;border-top:1px solid #CCC;border-bottom:1px solid #CCC;border-right:1px solid #CCC;}
#p_used_count{cursor:pointer;}
</style>
<script type="text/javascript">

$(document).ready(function(){
	$.ajax({
		url:"/action/favorite/attentionUsersCount",
		type:"get",
		data:"id="+37407+"&type="+5,
		success:function(msg){
			$("#p_favor_count").html("+"+msg);
		}
	});
	
	$('#favor_trigger').click(function(){
			add_to_favor(37407,5);
		});
});

var favor_ok = "<p class='favor_ok'>已成功添加到收藏夹<br/><br/> <a href='null/favorites?type=5'>我的收藏夹</a> | <a href='javascript:close_favor()'>关闭</a></p>";
function delete_favor(obi_id, obj_type){
	if(!confirm('确定取消收藏？')) return;
	$.post("/action/favorite/cancel?type="+obj_type+"&id="+obi_id+"&user=${g_user.id}",function(html){
    	$('#favor_trigger').removeClass('soft_collected');
		$('#favor_trigger').attr('title','添加到收藏');
    	$('#p_favor_count').html('+'+html);
		$('#favor_trigger').unbind('click');
		$('#favor_trigger').click(function(){add_to_favor(37407,5);});
	});
}
function add_to_favor(obj_id, obj_type){
	if(confirm('必须登录后才能收藏，是否现在登录？')){
		location.href="http://www.oschina.net/home/login?goto_page="+encodeURIComponent(location.href);
	}
}
function close_favor(elem_id){
    $('#favor_trigger').poshytip('destroy');
}
function setTag(tv){
	var t = $("._favor_input");
	var value = t.val();
	var tags = value.replace(/，/ig,',').split(',');
	var exist = false;
	value = $.map(tags,function(v,i){
		if($.trim(tv) == $.trim(v)){
			exist = true;
			return undefined;
		}
		return v;
	}).join(',');
	if(exist){
		t.val(value);
		return;
	}
	if(value!="")
		t.val(value+","+tv);
	else
		t.val(tv)
}
</script>
<div id="toolbar_wrapper">
	<div class="toolbar">
	<div class="tool_title"><b>分享到&nbsp;</b></div>
	<div class="share"><a class="share_sina" title="分享到新浪微博" href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=858381728',u=z||d.location,p=['&amp;url=',e(u),'&amp;title=',e(t||d.title),'&amp;source=',e(r),'&amp;sourceUrl=',e(l),'&amp;content=',c||'gb2312','&amp;pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','','Python%E6%93%8D%E4%BD%9CMongoDB(PyMongo%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8):%20','','utf-8'));">新浪微博</a><a class="share_qq" title="分享到腾讯微博" href="javascript:(function(){window.open('http://v.t.qq.com/share/share.php?url='+encodeURIComponent(document.location)+'&amp;appkey=96f54f97c4de46e393c4835a266207f4&amp;site=&amp;title='+encodeURIComponent(document.title)+encodeURIComponent(':%20'),'',%20'width=450,%20height=400,%20top=0,%20left=0,%20toolbar=no,%20menubar=no,%20scrollbars=no,%20location=yes,%20resizable=no,%20status=no');}())">腾讯微博</a></div>
	<div class="options">
				<div class="favorite">
		<a title="收藏此代码" id="favor_trigger" class="soft_collect " href="javascript:;">收藏</a>		<span class="soft_collect_count" id="p_favor_count">+6</span>
		</div>
						<div class="vote" id="Vote">
									<a id="vote_down" class="vote-down-off" href="javascript:;" title="踩：这代码不知道在说什么，或者没什么用">踩</a>
							<a id="vote_up" class="vote-up-off" href="javascript:;" title="顶：这代码很有用或者很清晰明了">顶</a>
					<span class="vote-count-post">0</span>
		</div>
			</div>
	</div>
</div>		
		<div class="Content">
			<div class="detail">
								PyMongo模块是Python对MongoDB操作的接口包，代码主要实现对MongoDB的几种操作：增删改查以及排序等功能，网上有很多资料，本文仅充当学习笔记。<div class="clear"></div></div>				
    		<div class="Tags">
    			<strong>标签：</strong>	
    											&lt;无&gt;
								    		</div>
    				</div>
    	<div class="clear"></div>
		<style>
			.CodeEntity .code_pieces ul.piece_anchor{width:25px;position:absolute;top:25px;left:-30px;z-index:1000;}
			.CodeEntity .code_pieces ul.piece_anchor li{width:25px;background: #efe;margin-bottom:2px;}
			.CodeEntity .code_pieces ul.piece_anchor li{border-left:3px #40AA63 solid;border-right:3px #efe solid;}
						.CodeEntity .code_pieces ul.piece_anchor li:hover{border-right:3px #40AA63 solid;border-left:3px #efe solid;}
			.CodeEntity .code_pieces ul.piece_anchor li a{color: #333;padding: 3px 10px;}
			.CodeEntity .code_pieces .jump_to_code{visibility:hidden;position:relative;}
			.CodeEntity .code_pieces .code_piece:hover .jump_to_code{visibility:visible;}
			.CodeEntity .code_pieces .code_piece:hover .jump_to_code a{text-decoration:none;}
			.CodeEntity .code_pieces h2 i{float:right;font-style:normal;font-weight:normal;}
			.CodeEntity .code_pieces h2 i a{font-size:9pt;background: #FFFFFF;color:#00A;padding: 2px 5px;text-decoration:none;}
		</style>
        <div class="CodeEntity">
        	<div class="code_pieces code_module">
				<!--<ul class="piece_anchor">-->
									<!--<li><a href="#${cp.id}" style="text-decoration:none;">$velocityCount</a></li>-->
								<!--</ul>-->
        		<h2>
					<a>代码片段<em>(1)</em></a>
					<i><a href="http://www.oschina.net/code/piece_full?code=37407" target="_blank">[全屏查看所有代码]</a></i>
				</h2>
            	            	<div class="code_piece">	
            	    				                	<h3><i>1.&nbsp;</i><i>[文件]</i> 
    					pymongo_operate.py&nbsp;~&nbsp;2KB&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="http://www.oschina.net/action/code/download?code=37407&amp;id=55031">下载</a><span style="font-weight:normal;font-size:8pt;">(2)</span>&nbsp;&nbsp;&nbsp;&nbsp;
    					<i class="jump_to_code">
    						    							<span>
        							<a>跳至</a>
        							    									    										    											<a href="#55031">[1]</a>
    										    									        							    							</span>
    						    							<a href="http://www.oschina.net/code/piece_full?code=37407&amp;piece=55031#55031" name="55031" target="_blank">[全屏预览]</a>
    					</i>
    				</h3>
            		                	<div><div id="highlighter_596141" class="syntaxhighlighter  python"><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div><div class="line number54 index53 alt1">54</div><div class="line number55 index54 alt2">55</div><div class="line number56 index55 alt1">56</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="python comments">#!/usr/bin/env python</code></div><div class="line number2 index1 alt1"><code class="python comments">#coding:utf-8</code></div><div class="line number3 index2 alt2"><code class="python comments"># Author:&nbsp;&nbsp; --&lt;qingfengkuyu&gt;</code></div><div class="line number4 index3 alt1"><code class="python comments"># Purpose: MongoDB的使用</code></div><div class="line number5 index4 alt2"><code class="python comments"># Created: 2014/4/14</code></div><div class="line number6 index5 alt1"><code class="python comments">#32位的版本最多只能存储2.5GB的数据（NoSQLFan：最大文件尺寸为2G，生产环境推荐64位）</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="python keyword">import</code> <code class="python plain">pymongo</code></div><div class="line number9 index8 alt2"><code class="python keyword">import</code> <code class="python plain">datetime</code></div><div class="line number10 index9 alt1"><code class="python keyword">import</code> <code class="python plain">random</code></div><div class="line number11 index10 alt2">&nbsp;</div><div class="line number12 index11 alt1"><code class="python comments">#创建连接</code></div><div class="line number13 index12 alt2"><code class="python plain">conn </code><code class="python keyword">=</code> <code class="python plain">pymongo.Connection(</code><code class="python string">'10.11.1.70'</code><code class="python plain">,</code><code class="python value">27017</code><code class="python plain">)</code></div><div class="line number14 index13 alt1"><code class="python comments">#连接数据库</code></div><div class="line number15 index14 alt2"><code class="python plain">db </code><code class="python keyword">=</code> <code class="python plain">conn.study</code></div><div class="line number16 index15 alt1"><code class="python comments">#db = conn['study']</code></div><div class="line number17 index16 alt2">&nbsp;</div><div class="line number18 index17 alt1"><code class="python comments">#打印所有聚集名称，连接聚集</code></div><div class="line number19 index18 alt2"><code class="python functions">print</code> <code class="python plain">u</code><code class="python string">'所有聚集:'</code><code class="python plain">,db.collection_names()</code></div><div class="line number20 index19 alt1"><code class="python plain">posts </code><code class="python keyword">=</code> <code class="python plain">db.post</code></div><div class="line number21 index20 alt2"><code class="python comments">#posts = db['post']</code></div><div class="line number22 index21 alt1"><code class="python functions">print</code> <code class="python plain">posts</code></div><div class="line number23 index22 alt2">&nbsp;</div><div class="line number24 index23 alt1"><code class="python comments">#插入记录</code></div><div class="line number25 index24 alt2"><code class="python plain">new_post </code><code class="python keyword">=</code> <code class="python plain">{</code><code class="python string">"AccountID"</code><code class="python plain">:</code><code class="python value">22</code><code class="python plain">,</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"libing"</code><code class="python plain">,</code><code class="python string">'date'</code><code class="python plain">:datetime.datetime.now()}</code></div><div class="line number26 index25 alt1"><code class="python plain">new_posts </code><code class="python keyword">=</code> <code class="python plain">[{</code><code class="python string">"AccountID"</code><code class="python plain">:</code><code class="python value">22</code><code class="python plain">,</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"liuw"</code><code class="python plain">,</code><code class="python string">'date'</code><code class="python plain">:datetime.datetime.now()},</code></div><div class="line number27 index26 alt2"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python plain">{</code><code class="python string">"AccountID"</code><code class="python plain">:</code><code class="python value">23</code><code class="python plain">,</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"urling"</code><code class="python plain">,</code><code class="python string">'date'</code><code class="python plain">:datetime.datetime.now()}]</code><code class="python comments">#每条记录插入时间都不一样</code></div><div class="line number28 index27 alt1">&nbsp;</div><div class="line number29 index28 alt2"><code class="python plain">posts.insert(new_post)</code></div><div class="line number30 index29 alt1"><code class="python comments">#posts.insert(new_posts)#批量插入多条数据</code></div><div class="line number31 index30 alt2">&nbsp;</div><div class="line number32 index31 alt1"><code class="python comments">#删除记录</code></div><div class="line number33 index32 alt2"><code class="python functions">print</code> <code class="python plain">u</code><code class="python string">'删除指定记录:\n'</code><code class="python plain">,posts.find_one({</code><code class="python string">"AccountID"</code><code class="python plain">:</code><code class="python value">22</code><code class="python plain">,</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"libing"</code><code class="python plain">})</code></div><div class="line number34 index33 alt1"><code class="python plain">posts.remove({</code><code class="python string">"AccountID"</code><code class="python plain">:</code><code class="python value">22</code><code class="python plain">,</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"libing"</code><code class="python plain">})</code></div><div class="line number35 index34 alt2">&nbsp;</div><div class="line number36 index35 alt1"><code class="python comments">#修改聚集内的记录</code></div><div class="line number37 index36 alt2"><code class="python plain">posts.update({</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"urling"</code><code class="python plain">},{</code><code class="python string">"$set"</code><code class="python plain">:{</code><code class="python string">'AccountID'</code><code class="python plain">:random.randint(</code><code class="python value">20</code><code class="python plain">,</code><code class="python value">50</code><code class="python plain">)}})</code></div><div class="line number38 index37 alt1">&nbsp;</div><div class="line number39 index38 alt2"><code class="python comments">#查询记录，统计记录数量</code></div><div class="line number40 index39 alt1"><code class="python functions">print</code> <code class="python plain">u</code><code class="python string">'记录总计为：'</code><code class="python plain">,posts.count(),posts.find().count()</code></div><div class="line number41 index40 alt2"><code class="python functions">print</code> <code class="python plain">u</code><code class="python string">'查询单条记录:\n'</code><code class="python plain">,posts.find_one()</code></div><div class="line number42 index41 alt1"><code class="python functions">print</code> <code class="python plain">posts.find_one({</code><code class="python string">"UserName"</code><code class="python plain">:</code><code class="python string">"liuw"</code><code class="python plain">})</code></div><div class="line number43 index42 alt2">&nbsp;</div><div class="line number44 index43 alt1"><code class="python comments">#查询所有记录</code></div><div class="line number45 index44 alt2"><code class="python functions">print</code> <code class="python plain">u</code><code class="python string">'查询多条记录:'</code></div><div class="line number46 index45 alt1"><code class="python comments">#for item in posts.find():#查询全部记录</code></div><div class="line number47 index46 alt2"><code class="python comments">#for item in posts.find({"UserName":"urling"}):#查询指定记录</code></div><div class="line number48 index47 alt1"><code class="python comments">#for item in posts.find().sort("UserName"):#查询结果根据UserName排序，默认为升序</code></div><div class="line number49 index48 alt2"><code class="python comments">#for item in posts.find().sort("UserName",pymongo.ASCENDING):#查询结果根据UserName排序，ASCENDING为升序,DESCENDING为降序</code></div><div class="line number50 index49 alt1"><code class="python keyword">for</code> <code class="python plain">item </code><code class="python keyword">in</code> <code class="python plain">posts.find().sort([(</code><code class="python string">"UserName"</code><code class="python plain">,pymongo.ASCENDING),(</code><code class="python string">'date'</code><code class="python plain">,pymongo.DESCENDING)]):</code><code class="python comments">#查询结果根据多列排序</code></div><div class="line number51 index50 alt2"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python functions">print</code> <code class="python plain">item</code></div><div class="line number52 index51 alt1">&nbsp;</div><div class="line number53 index52 alt2"><code class="python comments">#查看查询语句的性能</code></div><div class="line number54 index53 alt1"><code class="python comments">#posts.create_index([("UserName", pymongo.ASCENDING), ("date", pymongo.DESCENDING)])#加索引</code></div><div class="line number55 index54 alt2"><code class="python functions">print</code> <code class="python plain">posts.find().sort([(</code><code class="python string">"UserName"</code><code class="python plain">,pymongo.ASCENDING),(</code><code class="python string">'date'</code><code class="python plain">,pymongo.DESCENDING)]).explain()[</code><code class="python string">"cursor"</code><code class="python plain">]</code><code class="python comments">#未加索引用BasicCursor查询记录</code></div><div class="line number56 index55 alt1"><code class="python functions">print</code> <code class="python plain">posts.find().sort([(</code><code class="python string">"UserName"</code><code class="python plain">,pymongo.ASCENDING),(</code><code class="python string">'date'</code><code class="python plain">,pymongo.DESCENDING)]).explain()[</code><code class="python string">"nscanned"</code><code class="python plain">]</code><code class="python comments">#查询语句执行时查询的记录数</code></div></div></td></tr></tbody></table></div></div>
    				    				            	            	</div>
            	        	</div>
			<style type="text/css">
				.code_report{float:right}
				.code_report div{ float: left; margin-left: 5px; background: url("/img/ask-icon.gif") no-repeat; padding: 6px 0 6px 15px; padding-left: 15px; height: 16px;}
				.code_report a{ background: url("/img/ask-icon.gif") 0 -160px no-repeat;padding-left: 20px; line-height: 15px;height: 16px; color: #333; text-decoration: none; display:inline-block; zoom:1; vertical-align: middle;}
				.code_report em{ height: 28px; line-height:28px; width: 14px; display: inline-block; float: left; background: url("/img/ask-icon.gif") top right;}
            </style>
			<div class="code_report"><div><a href="javascript:report('http://www.oschina.net/code/snippet_1382328_37407',37407,5)">举报</a></div><em></em></div>
			<br>
			<br>
			<p style="margin:10px 0 0 0;font-weight:bold;color:#A00;text-decoration:none;">开源中国-程序员在线工具：<a href="http://git.oschina.net/" target="_blank">Git代码托管</a> <a href="http://tool.oschina.net/apidocs" style="text-decoration:none;">API文档大全(120+)</a> <a href="http://runjs.cn/" style="text-decoration:none;" target="_blank">JS在线编辑演示</a> <a href="http://tool.oschina.net/qr" style="text-decoration:none;" target="_blank">二维码</a>  <a href="http://tool.oschina.net/" style="text-decoration:none;" target="_blank">更多»</a></p>
			
        <div class="google_ads" style="margin:10px 0 20px 5px;">
            <script type="text/javascript" src="a_files/tag.js"></script><script async="" src="a_files/adsbygoogle.js"></script>
<!-- question-side -->
<ins data-adsbygoogle-status="done" class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-7090564139599510" data-ad-slot="5982720268"><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><iframe marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;" frameborder="0" height="90" width="728"></iframe></ins></ins></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

        </div>
		
        	        	        	<div id="related_codes" class="CodeList code_module">
        		<h2><a>相关的代码<em>(44)</em></a></h2>
                <ul>
                                                                	<li>
                		<span class="stat">0回/324阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1185023_35839" target="_blank" title="用python把CSV、EXCEL文件导入ORACLE数据库">用python把CSV、EXCEL文件导入ORACLE数据库</a>
                			<span class="date">2个月前 By highroom</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">0回/285阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1414453_34035" target="_blank" title="使用sqlite3模块的简单的数据库的添加查询，删除，修改程序">使用sqlite3模块的简单的数据库的添加查询，删除，修改程序</a>
                			<span class="date">4个月前 By benbentime</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">2回/151阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1165356_33688" target="_blank" title="Python获取java中jdbc rs读取代码">Python获取java中jdbc rs读取代码</a>
                			<span class="date">4个月前 By out_lier</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">0回/308阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1430119_27578" target="_blank" title="基于pickle实现大量高级功能的数据库">基于pickle实现大量高级功能的数据库</a>
                			<span class="date">7个月前 By ZuoDD</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">1回/1828阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1382328_27194" target="_blank" title="Python操作mysql（增删改查）">Python操作mysql（增删改查）</a>
                			<span class="date">7个月前 By 清风苦雨</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">0回/124阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_857488_26813" target="_blank" title="phpdisk-sql-injection.py">phpdisk-sql-injection.py</a>
                			<span class="date">8个月前 By Yaseng</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">5回/779阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_217347_24221" target="_blank" title="python连接mysql简单演示">python连接mysql简单演示</a>
                			<span class="date">11个月前 By 蓝宝石的傻话</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">1回/342阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_1244891_23701" target="_blank" title="zencart产品数据导入到magento">zencart产品数据导入到magento</a>
                			<span class="date">11个月前 By Alinzi</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                                	<li>
                		<span class="stat">2回/1070阅</span>
                		                		        					<span class="lang1">[Python]</span>
                		                		<span class="title">
                			<a href="http://www.oschina.net/code/snippet_925666_20043" target="_blank" title="python写的群发邮件系统 有点瑕疵 为了大家讨论下~~~">python写的群发邮件系统 有点瑕疵 为了大家讨论下~~~</a>
                			<span class="date">1年前 By 宁宁123韩</span>
                		</span>
        				<!--<div class='clear'></div>-->
                	</li>
                                                </ul>
        		<a href="http://www.oschina.net/code/list_releted_codes?id=37407">显示所有&nbsp;»</a>        	</div>
						        	<div class="code_comments code_module">
        		<h2>
        			<span class="right">
        			<a href="#commentform">发表评论</a>
        			<a href="#">回到顶部</a>
        			</span>
        			<a name="comments">网友评论<em>(1)</em></a>
        		</h2>
        		<div class="Comments">
        		            	<ul>
        		            	            	<li id="cmt_1459856_8194" class="row_1">
		<table width="100%"><tbody><tr>
	<td class="portrait">
		<a href="http://my.oschina.net/u/1459856" name="rpl_8194"><img src="a_files/portrait.gif" align="absmiddle"></a>
	</td>
	<td class="body">
		<div class="r_title">
			1楼：<b>newnoder</b> 发表于 2014-07-21 22:27 			<span class="opts">
												<a href="javascript:ReplyInline(1459856,8194)">回复此评论</a>
							</span>
		</div>
		<div class="r_content TextContent">mark，test下‘</div>
		<div id="inline_reply_of_1459856_8194" class="inline_reply"></div>
    </td></tr></tbody></table>
</li>            	            	</ul>
        			        		</div>
        	</div>
        	<div class="CommentForm" style="margin:0;">
        		<form id="form_code_comment" action="/action/code/comment" method="POST">
					<input name="code" value="37407" type="hidden">
					<input name="user" value="${g_user.id}" type="hidden">
        			<a name="commentform"></a><table>
        				
        				<tbody><tr>
        					<td><div style="width: 570px;" class="ke-container ke-container-oschina"><div unselectable="on" class="ke-toolbar" style="display:block;"><span class="ke-outline" data-name="fontname" title="字体" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-fontname" unselectable="on"></span></span><span class="ke-outline" data-name="fontsize" title="文字大小" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-fontsize" unselectable="on"></span></span><span class="ke-inline-block ke-separator"></span><span class="ke-outline" data-name="forecolor" title="文字颜色" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-forecolor" unselectable="on"></span></span><span class="ke-outline" data-name="hilitecolor" title="文字背景" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-hilitecolor" unselectable="on"></span></span><span class="ke-outline" data-name="bold" title="粗体(Ctrl+B)" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-bold" unselectable="on"></span></span><span class="ke-outline" data-name="italic" title="斜体(Ctrl+I)" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-italic" unselectable="on"></span></span><span class="ke-outline" data-name="underline" title="下划线(Ctrl+U)" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-underline" unselectable="on"></span></span><span class="ke-outline" data-name="removeformat" title="删除格式" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-removeformat" unselectable="on"></span></span><span class="ke-inline-block ke-separator"></span><span class="ke-outline" data-name="justifyleft" title="左对齐" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-justifyleft" unselectable="on"></span></span><span class="ke-outline" data-name="justifycenter" title="居中" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-justifycenter" unselectable="on"></span></span><span class="ke-outline" data-name="justifyright" title="右对齐" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-justifyright" unselectable="on"></span></span><span class="ke-outline" data-name="insertorderedlist" title="编号" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-insertorderedlist" unselectable="on"></span></span><span class="ke-outline" data-name="insertunorderedlist" title="项目符号" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-insertunorderedlist" unselectable="on"></span></span><span class="ke-inline-block ke-separator"></span><span class="ke-outline" data-name="emoticons" title="插入表情" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-emoticons" unselectable="on"></span></span><span class="ke-outline" data-name="link" title="超级链接" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-link" unselectable="on"></span></span><span class="ke-outline" data-name="unlink" title="取消超级链接" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-unlink" unselectable="on"></span></span><span class="ke-outline" data-name="source" title="HTML代码" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-source" unselectable="on"></span></span><span class="ke-outline" data-name="about" title="关于" unselectable="on"><span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-about" unselectable="on"></span></span></div><div class="ke-edit" style="display: block; height: 111px;"><iframe style="width: 100%; height: 111px;" class="ke-edit-iframe" hidefocus="true" frameborder="0"></iframe><textarea style="width: 100%; height: 111px; display: none;" class="ke-edit-textarea" hidefocus="true"></textarea></div><div class="ke-statusbar"><span class="ke-inline-block ke-statusbar-center-icon"></span><span class="ke-inline-block ke-statusbar-right-icon"></span></div></div><textarea id="ta_code_comment" name="content" style="width: 600px; height: 150px; visibility: hidden; display: none;"></textarea></td>
        				</tr>
        				<tr class="buttons">
        					<td>
        						<input value="发表评论" class="BUTTON SUBMIT rndbutton" type="submit">
        						<span id="comment_submit_msg" class="code_error_msg"></span>
        					</td>		
        				</tr>
        				<tr><td></td></tr>
        			</tbody></table>
        		</form>
        		<a href="#" class="more">回到顶部</a>&nbsp;&nbsp;
        		<a href="#comments" class="more">回到评论列表</a>
        	</div>
        </div>
	</div>
    <div class="QuestionRelations">
		
		<div id="QuestionWizard">
			开源从代码分享开始
			<a href="http://www.oschina.net/code/step1" class="rndbutton"><span>分享代码</span></a>
			<div class="clear"></div>
		</div>		
		<div style="text-align:center;">
			<script async="" src="a_files/adsbygoogle.js"></script>
            <!-- news-side -->
            <ins data-adsbygoogle-status="done" class="adsbygoogle" style="display:inline-block;width:300px;height:250px" data-ad-client="ca-pub-1307862221338762" data-ad-slot="5950287625"><ins style="display:inline-table;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><ins id="aswift_1_anchor" style="display:block;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><iframe marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;" frameborder="0" height="250" width="300"></iframe></ins></ins></ins>
            <script>
            (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
					</div>
		
				<div id="OtherQuestionsOfUser" class="Qlist">
			<strong><em>清风苦雨</em>的其他代码</strong>
			<ul>
						<li><a href="http://www.oschina.net/code/snippet_1382328_37407" title="Python操作MongoDB(PyMongo模块的使用)">Python操作MongoDB(PyMongo模块的使用)</a><span class="date">(1评/187阅,6天前)</span></li>						<li><a href="http://www.oschina.net/code/snippet_1382328_27194" title="Python操作mysql（增删改查）">Python操作mysql（增删改查）</a><span class="date">(1评/1828阅,7个月前)</span></li>						</ul>
			<a href="http://www.oschina.net/code/list_by_user?id=1382328" class="more">全部(2)...</a>
		</div>
		<div style="text-align:center;margin-bottom:20px;">
			<script type="text/javascript" src="a_files/code.js"></script><script type="text/javascript"><!--
google_ad_client = "ca-pub-7090564139599510";
/* 728x90, 创建于 10-8-28 */
google_ad_slot = "3057306568";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript" src="a_files/show_ads.js">
</script><ins style="display:inline-table;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><ins id="aswift_2_anchor" style="display:block;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><iframe marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_2" name="aswift_2" style="left:0;position:absolute;top:0;" frameborder="0" height="250" width="300"></iframe></ins></ins>

		</div>		
    </div>
	<div class="clear"></div>   
</div>
<div id="inline_reply_editor" style="display:none;">
<div class="CommentForm" style="padding-left:0px;">
	<form id="form_inline_comment" action="/action/code/comment?code=37407&amp;user=${g_user.id}" method="POST">
	  <input id="inline_reply_id" name="reply_id" value="" type="hidden">
		<table>
						<tbody><tr height="40">
				<td>
					邮箱或帐号：<input name="email" class="TEXT" size="20" type="text">
					登录密码：<input name="pwd" class="TEXT" size="10" type="password">
					还没帐号呢，<a href="http://www.oschina.net/home/reg" target="_blank">现在注册一个？</a>
				</td>
			</tr>
						<tr>
				<td><textarea name="content" style="width:500px;height:80px;"></textarea></td>
			</tr>
			<tr class="buttons">
				<td>
					<input value="发表评论" class="BUTTON SUBMIT" type="submit">
					<input value="关闭" class="SUBMIT" id="btn_close_inline_reply" type="button">
				</td>		
			</tr>
			<tr><td></td></tr>
		</tbody></table>
	</form>
</div>
</div>
<!--[if lt IE 7]>
<script type="text/javascript" src="/js/minmax.js"></script>
<![endif]-->
<script type="text/javascript" src="a_files/code" defer="defer"></script>
<script charset="utf-8" src="a_files/kindeditor-min.js"></script>

<script type="text/javascript" src="a_files/xregexp-min.js"></script>
<script type="text/javascript" src="a_files/shCore.js"></script>
<script type="text/javascript" src="a_files/shAutoloader.js"></script>
<link type="text/css" rel="stylesheet" href="a_files/shCore.css">
<link type="text/css" rel="Stylesheet" href="a_files/shThemeDefault.css">

<script type="text/javascript">
SyntaxHighlighter.autoloader(
	'applescript			/js/sh309/scripts/shBrushAppleScript.js',
	'actionscript3 as3		/js/sh309/scripts/shBrushAS3.js',
	'bash shell				/js/sh309/scripts/shBrushBash.js',
	'coldfusion cf			/js/sh309/scripts/shBrushColdFusion.js',
	'cpp c					/js/sh309/scripts/shBrushCpp.js',
	'obj-c objc				/js/sh309/scripts/shBrushObjC.js',
	'c# c-sharp csharp		/js/sh309/scripts/shBrushCSharp.js',
	'css					/js/sh309/scripts/shBrushCss.js',
	'delphi pascal			/js/sh309/scripts/shBrushDelphi.js',
	'diff patch pas			/js/sh309/scripts/shBrushDiff.js',
	'erl erlang				/js/sh309/scripts/shBrushErlang.js',
	'groovy					/js/sh309/scripts/shBrushGroovy.js',
	'haxe hx				/js/sh309/scripts/shBrushHaxe.js',
	'java					/js/sh309/scripts/shBrushJava.js',
	'jfx javafx				/js/sh309/scripts/shBrushJavaFX.js',
	'js jscript javascript	/js/sh309/scripts/shBrushJScript.js',
	'perl pl				/js/sh309/scripts/shBrushPerl.js',
	'php					/js/sh309/scripts/shBrushPhp.js',
	'text plain				/js/sh309/scripts/shBrushPlain.js',
	'py python				/js/sh309/scripts/shBrushPython.js',
	'ruby rails ror rb		/js/sh309/scripts/shBrushRuby.js',
	'scala					/js/sh309/scripts/shBrushScala.js',
	'sql					/js/sh309/scripts/shBrushSql.js',
	'vb vbnet				/js/sh309/scripts/shBrushVb.js',
	'xml xhtml xslt html	/js/sh309/scripts/shBrushXml.js'
);
SyntaxHighlighter.all();
</script>
<script type="text/javascript"><!--
function pay_attention(pid,concern_it){
	login_and_do(function(msg){
		if(concern_it){
			$("#p_attention_count").load("/action/favorite/add?mailnotify=true&type=5&id="+pid, {user: ''});
			$('#attention_it').html('<a href="javascript:pay_attention('+pid+',false)" style="color:#A00;">取消收藏</a>');	
		}
		else{
			$("#p_attention_count").load("/action/favorite/cancel?type=5&id="+pid, {user: ''});
			$('#attention_it').html('<a href="javascript:pay_attention('+pid+',true)" style="color:#3E62A6;">收藏此代码</a>');
		}
		tb_remove();
	});
}

function close_tip(tid){$('#'+tid).poshytip('destroy');}
function vote_question(qid, vote_up, need_confirm){
	if(need_confirm && !vote_up){
		if($('#Vote #vote_down').hasClass('vote-down-off')){
			var vote_down_confirm_msg = "<p>踩代码将会扣掉你1个积分，是否继续？</p><p style='margin-top:10px;'><a href='javascript:vote_question(37407,false,false)' class='rbtn' style='margin-right:10px;'><span>确定</span></a><a href=\"javascript:close_tip('vote_down')\" class='rbtn'><span>取消</span></a></p>";
			$('#Vote #vote_down').poshytip({
				className: 'tip-yellowsimple',
				content: vote_down_confirm_msg,
				slide: false,
				fade: false,
				showOn: 'none',
				alignTo: 'target',
				alignX: 'inner-right',
				alignY: 'bottom',
				offsetX: -30,
				offsetY: 15
			});
			$('#Vote #vote_down').poshytip('show');
			return;
		}
	}
	if(!need_confirm){
		$('#Vote #vote_down').poshytip('destroy');
	}
	ajax_post("/action/code/vote?user=${g_user.id}&id="+qid+"&vote="+vote_up,"",function(data){
		json = eval('('+data+')');
		if(json.msg){
			var aid = vote_up?"vote_up":"vote_down";
			$('#'+aid).poshytip({
				className: 'tip-yellowsimple',
				content: json.msg,
				showOn: 'none',
				alignTo: 'target',
				alignX: 'inner-right',
				alignY: 'bottom',
				offsetX: 5,
				offsetX: -35
			});
			$('#'+aid).poshytip('show');
			var t = setTimeout(function(){
				clearTimeout(t);
				$('#'+aid).poshytip('destroy');
			},2000);
		}
		else{
			$('#Vote .vote-count-post').html(json.vote);
			if(vote_up){
				$('#Vote #vote_up').toggleClass('vote-up-off');
				$('#Vote #vote_up').toggleClass('vote-up-on');
			}
			else{
				$('#Vote #vote_down').toggleClass('vote-down-off');
				$('#Vote #vote_down').toggleClass('vote-down-on');
			}
		}
	});
}
function delete_c(code,uid,cid){
  if(confirm("您确认要删除此篇评论？")){
    var args = "cmt="+cid+"#"+uid+"#"+code;
    ajax_post("/action/code/delete_comments",args,function(){$("#cmt_"+uid+"_"+cid).fadeOut();});
  }
}
function ReplyInline(user,reply){
	$('.inline_reply').empty();
	var div_id = '#inline_reply_of_'+user+'_'+reply;
	$('#inline_reply_id').val(user+'_'+reply);
	$(div_id).html($('#inline_reply_editor').html());
	$('#txt_focus').focus();
	$('#btn_close_inline_reply').click(function(){
		$(div_id).empty();
	});
	$('#form_inline_comment').ajaxForm({
	dataType: 'json',
	success: function(json) {
    	if(json.msg){
    		alert(json.msg);
    	}
    	else if(json.id){
			location.reload();
    	}
	}
});
}

function edit_code_tags(code_id){
	popup("/code/edit_tags?code="+code_id);
}

var editor;
KindEditor.ready(function(K) {
    editor = K.create('#ta_code_comment', {
		themeType : 'oschina',
		width:'570px',
		resizeType : 2,
		urlType: 'domain',
		shadowMode : false,
		cssPath : '/css/ke-oschina.css',
		allowPreviewEmoticons : false,
		newlineTag : 'br',
		afterCreate : function(){
			K.ctrl(this.edit.iframe.get(0).contentWindow.document, 13, function() {
				$("#ta_code_comment").parent().submit();
			});
		},
		afterChange : function() {
			this.sync();
		},
		items : [
		'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
		'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
		'insertunorderedlist', '|', 'emoticons', 'link','unlink','source','about']
    });
});

$(document).ready(function(){
    $('#form_code_comment').ajaxForm({
		dataType: 'json',
		beforeSerialize: function($form, options) { 
			editor.sync();           
        },
        success: function(json) {
        	if(json.msg){
        		$('#comment_submit_msg').html(json.msg);
    			$('#comment_submit_msg').css('display','inline');
        	}else{
        		jQuery.get("/code/show_reply?_cmt_user="+json.user+"&_cmt_id="+json.id, function(data){
    				$('.Comments .NoData').hide();
        			$('.Comments ul').eq(0).append(data);
					editor.html('');
        		}); 
        	}
        }
    });
	
	
    //问题投票 
    $('#vote_up').click(function(){
    	if(this.clickTimeout){
            // 双击
            clearTimeout(this.clickTimeout);
            this.clickTimeout = null;
    		alert("不用那么费劲啦，点击一下就够了:)");
        }
        else{
            // 单击
            var elem = this;
            this.clickTimeout = setTimeout(function(){
                // 执行点击动作
                elem.clickTimeout = null;
    			vote_question(37407,true, true);
            }, 250);
        }
        //阻止链接onclick时的默认行为
        return false;
    });
    $('#vote_down').click(function(){
    	vote_question(37407,false, true);
        return false;
    });
});
function edit_catalogs(qid){
	popup("/admin/catalog/set-catalogs?parent=1&type=5&id="+qid);
}
//-->
</script>
<div class="clear"></div></div>
	<div id="OSC_Footer" class="CenterDiv"><style>
.oscapp {text-align:left; width:220px;}
.oscapp span {float:left;width:140px;}
.oscapp a {float:left;text-indent:-9999em;width:16px;margin-left:8px;}
.oscapp a.android {background:url('/img/android.gif') no-repeat left center;}
.oscapp a.iphone {background:url('/img/iphone.gif') no-repeat left center;}
.oscapp a.wp7 {background:url('/img/wp7.gif') no-repeat left center;}
</style>
<table width="100%"><tbody><tr>
<td align="left">© 开源中国(OsChina.NET) | <a href="http://www.oschina.net/home/aboutosc">关于我们</a> | <a href="mailto:oschina.net@gmail.com">广告联系</a> | <a href="http://weibo.com/oschina2010" target="_blank">@新浪微博</a> | <a href="http://m.oschina.net/">开源中国手机版</a> | <a href="http://www.miitbeian.gov.cn/" target="_blank" style="color:#737573;text-decoration:none;">粤ICP备12009483号-3</a></td>
<td class="oscapp">
	<span>开源中国手机客户端：</span>
	<a href="http://www.oschina.net/app" class="android" title="Android客户端">Android</a>
	<a href="http://www.oschina.net/app" class="iphone" title="iPhone 客户端">iPhone</a>
	<a href="http://www.oschina.net/app" class="wp7" title="Windows Phone 客户端">WP7</a>
</td>
</tr>
<tr>
<td colspan="2" align="left">开源中国社区(OSChina.net)是工信部 <a href="http://www.copu.org.cn/" target="_blank">开源软件推进联盟</a> 的官方社区</td>
</tr>
</tbody></table>
<script type="text/javascript">
<!--
if (top.location != self.location)top.location=self.location;
//-->
</script></div>
</div>


<div id="fancybox-tmp"></div><div id="fancybox-loading"><div></div></div><div id="fancybox-overlay"></div><div id="fancybox-wrap"><div id="fancybox-outer"><div class="fancybox-bg" id="fancybox-bg-n"></div><div class="fancybox-bg" id="fancybox-bg-ne"></div><div class="fancybox-bg" id="fancybox-bg-e"></div><div class="fancybox-bg" id="fancybox-bg-se"></div><div class="fancybox-bg" id="fancybox-bg-s"></div><div class="fancybox-bg" id="fancybox-bg-sw"></div><div class="fancybox-bg" id="fancybox-bg-w"></div><div class="fancybox-bg" id="fancybox-bg-nw"></div><div id="fancybox-content"></div><a id="fancybox-close"></a><div id="fancybox-title"></div><a href="javascript:;" id="fancybox-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a><a href="javascript:;" id="fancybox-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a></div></div><div title="回到顶部" style="position: fixed; bottom: 120px; right: 100px; cursor: pointer; display: none;" id="topcontrol"><img src="a_files/gotop.gif" style="width:31px; height:31px;"></div></body></html>
<!-- Generated by OsChina.NET (init:1[ms],page:36[ms],ip:113.116.78.80) -->